import pymysql.cursors


class mysql_utils:

    def __init__(self, strsql, dbname,host,port,user,passwd):
        self.str_sql = strsql
        self.db_name = dbname
        self.str_host=host
        self.str_user=user
        self.str_passwd=passwd

    def connect_mysql(self):
        conn = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            passwd='123456')
        conn.ping(True)
        cur = conn.cursor()
        conn.set_charset("utf8")
        conn.select_db(self.db_name)
        cur.execute(self.str_sql)
        # 获取剩余结果的第一行数据
        row1 = cur.fetchone()
        print(row1)

        # 获取剩余结果所有数据
        # rows=cur.fetchall()
        # for dr in rows:
        #     print("fetchall"+dr)

        cur.close()
        conn.close()
